/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.service;

import com.hiof.moraa.mapping.TblAthlete;
import com.hiof.moraa.mapping.TblSupport;
import com.hiof.moraa.server.dao.SupportDAO;
import com.hiof.moraa.server.dao.SupportLiveMediaDAO;
import com.hiof.moraa.server.service.model.LiveMediaFeed;
import com.hiof.moraa.server.service.model.SupportLiveMediaFeed;
import com.hiof.moraa.server.service.model.TypeOfAttendant;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author manish
 */
@Transactional(readOnly = false)
public class SupportLiveMediaServiceImp extends LiveMediaService {

    SupportLiveMediaDAO dao;
    MessageDeliveryService messageDeliveryService;
    SupportDAO supportDAO;

    public void setMessageDeliveryService(MessageDeliveryService messageDeliveryService) {
        this.messageDeliveryService = messageDeliveryService;
    }

    public void setDao(SupportLiveMediaDAO dao) {
        this.dao = dao;
    }

    public  void setSupportDAO(SupportDAO supportDAO){
        this.supportDAO = supportDAO;
    }

    @Override
    public LiveMediaFeed processLiveMedia(int userId, int supportId, MultipartFile file, double lat, double lng, double height, ServletContext servletContext) {
        TblSupport support = supportDAO.getSupportById(supportId);

        String username = support.getTblUser().getUsername();
        String routeName = support.getTblRoute().getName();
        String relativePath = getRelativeFilePath(username, routeName, file);

        String absolutePath = getFilePathService().getAbsolutePath(relativePath, servletContext);
        try {
            writeFile(absolutePath, file);
        } catch (IOException ex) {
            Logger.getLogger(AthleteLiveMediaServiceImp.class.getName()).log(Level.SEVERE, null, ex);
        }
        SupportLiveMediaFeed liveMediaFeed = dao.insertSupportLiveMediaFeed(supportId, getFilePathService().getResourcePath(relativePath), lat, lng, height, new Timestamp(new java.util.Date().getTime()));

        messageDeliveryService.deliveryLiveMediaMessage(userId, TypeOfAttendant.SUPPORT, supportId, liveMediaFeed);
        return liveMediaFeed;
    }
}
